package cn.tedu.eguser.controller;

import cn.tedu.eguser.mapper.UserMapper;
import cn.tedu.eguser.pojo.dto.UserAddParam;
import cn.tedu.eguser.pojo.dto.UserUpdateParam;
import cn.tedu.eguser.pojo.entity.User;
import cn.tedu.eguser.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("add")
    public String insertUser(@RequestBody UserAddParam userAddParam){
        System.out.println("添加用户" + userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        System.out.println(user);
        int result = userMapper.insertUser(user);
        return "添加用户成功";
    }

    @GetMapping("list")
    public List<UserListVO> listUser(String  username){
        List<UserListVO> list = userMapper.selectUserList(username);
        return list;
    }

    @PostMapping("del")
    public String delUser(Long id){
        int result = userMapper.deleteUserById(id);
        if (result <= 0){
            return "删除用户失败，用户不存在";
        }
        return "删除用户成功";
    }

    @PostMapping("update")
    public String updateUser(@RequestBody UserUpdateParam userUpdateParam){
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        int result = userMapper.updateUserById(user);
        return "修改用户成功";
    }
}
